class Solution(object):
    def validUtf8(self, data):
        """
        :type data: List[int]
        :rtype: bool
        """
        n = len(data)
        index = 0
        base = 192
        while index < n:
            wei = 0
            while wei < 5:
                if data[index] >> (7 - wei) & 1 == 0:
                    break
                else:
                    wei += 1
            if wei == 5:
                return False
            if wei == 0:
                index += 1
            else:
                for k in range(1, wei):
                    if index + k >= n:
                        return False
                    else:
                        if data[index + k] & base != 128:
                            return False
                index += wei
        return True





d = Solution()
data = [235, 140, 4]
print(d.validUtf8(data))
